package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.CustomerAuthorDefaultData;
import com.zb.entity.RoleAuthorDefaultData;
import com.zb.mapper.RoleAuthorDefaultDataMapper;
import com.zb.service.RoleAuthorDefaultDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author WEN
 * @Date 2024/5/8 0008
 **/
@Service
public class RoleAuthorDefaultDataServiceImpl extends ServiceImpl<RoleAuthorDefaultDataMapper, RoleAuthorDefaultData> implements RoleAuthorDefaultDataService {
    @Resource
    private RoleAuthorDefaultDataMapper roleAuthorDefaultDataMapper;

    // 保存角色权限
    @Override
    public void saveRoleAuthor(Long custAccId, List<CustomerAuthorDefaultData> authorDefaultDataList) {
        if (CollectionUtil.isEmpty(authorDefaultDataList)) {
            return;
        }
        // 获取注册企业时创建的两个角色
        List<Long> roleIds = roleAuthorDefaultDataMapper.getRoleIdByCustAccId(custAccId);
        List<RoleAuthorDefaultData> roleAuthorDefaultDataList = new ArrayList<>();
        RoleAuthorDefaultData roleAuthor = null;
        for (Long roleId : roleIds) {
            for (CustomerAuthorDefaultData author : authorDefaultDataList) {
                roleAuthor = new RoleAuthorDefaultData();
                roleAuthor.setId(IdWorker.getId());
                roleAuthor.setCustAccId(custAccId);
                roleAuthor.setRoleId(roleId);
                roleAuthor.setAuthorId(author.getAuthorId());
                roleAuthor.setParentAuthorId(author.getParentAuthorId());
                roleAuthorDefaultDataList.add(roleAuthor);
            }
        }
        this.saveBatch(roleAuthorDefaultDataList);
    }
}
